Gives the average of the cells in a block of target - from column x1, row y1 to column x2, row y2, both corners included. On a grid of whole numbers the answer is a whole number and it TRUNCATES: the part after the point is dropped, toward zero, so the average of 1 and 2 is 1 and the average of -1 and -2 is -1. On a grid of decimals the answer keeps its fraction. A block with nothing in it gives 0. A grid of text, yes-or-no values or structs is refused when the project is checked.
grid_get_region_mean(target, x1, y1, x2, y2) -> element| Name | Type |
|---|---|
target | grid |
x1 | int |
y1 | int |
x2 | int |
y2 | int |
element
Gives the average of the cells in a block of target, a grid: they are all added up and the total is divided by how many cells there are. The block runs from column x1, row y1 to column x2, row y2, and both corners are included: (1, 1) to (3, 2) is 6 cells.
var average = grid_get_region_mean(scores, 0, 0, 3, 2);The block follows the same rules as grid_fill_region, which has the whole story. In short: both corners are included, corners the wrong way round make an empty block, a block that runs off the grid is cut to the grid, and a corner typed as a number that is off the grid is refused when the project is checked.
On a grid of whole numbers, the answer is a whole number, and it is truncated: the part after the point is dropped, toward zero. It is not rounded.
| The cells | The real average | grid_get_region_mean gives |
|---|---|---|
| 1 and 2 | 1.5 | 1 |
| 1 and 1 and 2 | 1.33... | 1 |
| 2 and 3 and 4 | 3 exactly | 3 |
| 0 and 1 | 0.5 | 0 |
| -1 and -2 | -1.5 | -1 (toward zero, not -2) |
If you need the fraction, use a grid of decimals instead (grid_create(8, 6, 0.0)). On a grid of decimals the answer keeps its fraction, down to the smallest step a decimal has: the average of 1.0 and 2.0 is 1.5.
If the corners are the wrong way round, or the block is wholly off the grid, there are no cells, and dividing by zero cells is not possible, so the answer is 0 (or 0.0 for a grid of decimals). This can only happen with numbers that are worked out while the game runs. A mistake typed straight into the code is refused when checked.
grid_get_region_mean needs a grid of whole numbers (grid_create(8, 6, 0)) or of decimals (grid_create(8, 6, 0.0)). A grid of text, of yes-or-no values, of structs or of references has no average, so it does not build: the project is refused when it is checked, with a message that says what the grid holds. It is a message from GBML, not an error from the compiler later on.
The adding up is done in a bigger number than a cell holds, and the answer is never bigger than the biggest cell in the block, so the average of a block of huge numbers is right even when their total would be too big for a whole number. (The total, if you ask for it with grid_get_region_sum, stops at the biggest number instead.)
Like grid_fill_region, it needs to know how many cells are in a row. A grid with no size written down, or a script's grid parameter, is refused. Do the averaging in the object that made the grid and hand the script the answer.
| You do this | What happens |
|---|---|
| use it on a grid of text, yes-or-no, structs or references | refused when checked |
give a corner with a decimal point, like 1.5 | refused when checked. A corner is a whole number |
| give it a list or an array | refused when checked |
| give it the wrong number of arguments | refused when checked |
| a block that runs off the grid | only the cells on the grid are counted, and the average is of those |
It visits every cell in the block once, and then does one division, so the time grows with the number of cells in the block. It does not change the grid.
var scores = grid_create(4, 3, 0);
scores[# 0, 0] = 1;
scores[# 1, 0] = 2;
var average = grid_get_region_mean(scores, 0, 0, 1, 0); // 1 and 2 is 1.5, which is cut off to 1
var whole = grid_get_region_mean(scores, 0, 0, 3, 2); // 3 over 12 cells is 0.25, cut off to 0Two loops over the block, adding up exactly in 64 bits, then one 64-bit division by how many cells were in the block - O(cells in the block) plus that one division. The division is C++'s integer division, which drops the fraction toward zero; on a real grid it divides the exact 64-bit total of the raw fixed-point numbers, so the result is cut off only at the smallest step a fixed-point number has (1/4096). The answer is never bigger than the biggest cell in the block, so it cannot overflow however large the cells are. A block with no cells would divide by zero, so it answers 0 instead. It needs the grid's width, which a flat vector does not carry, so it takes the width and height the checker reads from the grid's declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes them into the C++ as plain numbers - the same two numbers [# x, y] uses. A grid with none (a script's parameter, one declared with no shape) is refused. The four numbers are worked out once each, in the order written, then held to 0..width-1 and 0..height-1, so the loops cannot leave the grid and a block that runs off one edge never wraps onto the next row; the flat index is also checked against the vector's real size. Numbers typed straight into the call that are off the grid, or corners typed the wrong way round, are refused when the project is checked. Only int and real grids build.